module.exports = app => {
  const express = require('express')
  const db = require('mongoose')
  const token = require('./token')
  const router = express.Router()
  app.use(express.json()) // post解析json

  /**
 * @api {post} /login 用户登录
 * @apiDescription 用户登录
 * @apiName login
 * @apiGroup User
 * @apiParam {string} user 用户名
 * @apiParam {string} pass 密码
 * @apiSuccess {json} result
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "msg" : "success",
 *      "code": 0
 *  }
 * @apiSampleRequest http://localhost:3003/node/api/user/login
 * @apiVersion 0.0.0
 */

  // 收藏改变
  router.put('/collect', async (req, res) => {
    const data = await await user.findOne({
      user: req.body.user
    })
    const item = data.collectList.find(item => { return item === req.body.collect })
    if (item) {
      data.collectList.remove(req.body.collect)
    } else {
      data.collectList.push(req.body.collect)
    }
    await user.findByIdAndUpdate(data._id, {
      ...data,
      collectList: data.collectList
    })
    res.send({
      code: 0,
      msg: 'success'
    })
  })
  
  // 修改
  router.put('/logoInfo', async (req, res) => {
    const isuser = await user.findOne({
      user: req.body.data.user
    })
    if (isuser) {
      const data = {
        ...req.body.data,
        user: isuser.user,
        upDate: new Date()
      }
      await user.findByIdAndUpdate(isuser.id, data)
      res.send({
        code: 0,
        msg: 'success'
      })
    } else {
      res.send({ code: 1, msg: '修改失败' })
    }
  })
  
  // 查询
  router.get('/logoInfo/:user', async (req, res) => {
    const isuser = await user.findOne({
      user: req.params.user
    })
    if (isuser) {
      res.send({
        code: 0,
        data: isuser,
        msg: 'success'
      })
    } else {
      res.send({
        code: 1,
        msg: '查询为空'
      })
    }
  })

  // 登录
  router.post('/login', async (req, res) => {
    const isuser = await user.findOne({
      user: req.body.data.user
    })
    console.log(isuser, req.body.data)
    if (!isuser) { // 没有用户自动注册
      user.create({ ...req.body.data, createDate: new Date() })
      return res.send({
        code: 2,
        data: req.body.data,
        msg: '注册成功'
      })
    } else {
      if (isuser.pass !== req.body.data.pass) {
        return res.send({ code: 1, msg: '密码错误' })
      }
      const data = {
        photo: isuser.photo,
        gender: isuser.gender,
        birthday: isuser.birthday,
        name: isuser.name,
        collectList: isuser.collectList,
        user: isuser.user
      }
      res.send({
        code: 0,
        token: token.createToken({ _id: isuser._id }),
        data,
        msg: 'success'
      })
    }
  })

  // 建表测试
  const user = db.model('user', new db.Schema({
    user: {
      type: String,
      unique: true // 字段是否唯一
    },
    pass: {
      type: String
    },
    name: { type: String },
    photo: { type: String },
    gender: { type: String },
    birthday: { type: String },
    upDate: { type: String },
    collectList: { type: Array },
    createDate: { type: String }
  }))

  app.use('/node/api/user', router)
}
